<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
 * 对于发布信息的控制器
 */
class Post extends Controller {
    function Post() {
        parent::Controller();
        $this->load->model("post_model");
        $this->load->model("book_model");
        $this->load->model("user_model");
        $this->load->helper('url');
        $this->load->library('mypagination');
    }

    /**
     * 获取对于某本书的出售和求购信息
     */
    function getpost_ofbook(){
        $bookid=$_GET['bookid'];
        list($schoolsetstatus,$school)=$this->user_model->get_schoolinitial();
        $data['salepost']=$this->post_model->get_salepost_ofbook($school,$bookid,0,0,15);
        $data['wantedpost']=$this->post_model->get_wantedpost_ofbook($school,$bookid,0,0,15);
        $data['book']=$this->book_model-> get_book_detail($bookid);
        $data['title']="书籍求购及待售信息-书易";
        $data['activetab']="none";
        $data['user_status']=$this->user_model->get_user_status(); //获取用户登录状态
        $this->load->view('post/postof_book',$data);
    }

    /**
     * 获取出售的单本书籍广告信息细节.包括图书信息和卖家信息。
     */
    function salepost_detail(){
        $userid=$_GET['userid'];
        $bookid=$_GET['bookid'];
        list($schoolsetstatus,$school)=$this->user_model->get_schoolinitial();
        $data['post']=$this->post_model->get_salepost_detail($school,$userid,$bookid);
        $data['book']=$this->book_model-> get_book_detail($bookid);
        if(isset($_GET['contact'])){
            if($this->session->userdata('username')==""){
               $siteurl=site_url();
               $currentPage=$_SERVER['SERVER_NAME'].$_SERVER['PHP_SELF'].'?'.$_SERVER['QUERY_STRING'];
               $loginPage=$siteurl.'/user/login?continue=yes&currentpage='.urlencode($currentPage);
               header("Location: $loginPage");
            }
          $data['contact']=$_GET['contact'];
        }
        else $data['contact']="none";
        $data['title']="卖出书籍详细-书易";
        $data['activetab']="none";
        $data['user_status']=$this->user_model->get_user_status(); //获取用户登录状态
        $this->load->view('post/salepostdetail',$data);
    }

     /**
     * 获取求购的单本书籍广告信息细节.包括图书信息和买家信息。
     */
     function wantedpost_detail(){
        $userid=$_GET['userid'];
        $bookid=$_GET['bookid'];
        list($schoolsetstatus,$school)=$this->user_model->get_schoolinitial();
        $data['post']=$this->post_model->get_wantedpost_detail($school,$userid,$bookid);
        if(isset($_GET['contact'])){
            if($this->session->userdata('username')==""){
               $siteurl=site_url();
               $currentPage=$_SERVER['SERVER_NAME'].$_SERVER['PHP_SELF'].'?'.$_SERVER['QUERY_STRING'];
               $loginPage=$siteurl.'/user/login?continue=yes&currentpage='.urlencode($currentPage);
               header("Location: $loginPage");
            }
          $data['contact']=$_GET['contact'];
        }
        else $data['contact']="none";
        $data['title']="求购书籍详细-书易";
        $data['book']=$this->book_model-> get_book_detail($bookid);
        $data['activetab']="none";
        $data['user_status']=$this->user_model->get_user_status(); //获取用户登录状态
        $this->load->view('post/wantedpostdetail',$data);
    }

    /**
     * 调用添加书籍出售广告的控制器
     */
    function salepost(){
        $this->load->library('session');
        $siteurl=site_url();
        $currentPage=$_SERVER['SERVER_NAME'].$_SERVER['PHP_SELF'].'?'.$_SERVER['QUERY_STRING'];
        if(($this->session->userdata('username'))==""){
            $loginPage=$siteurl.'/user/login?continue=yes&currentpage='.urlencode($currentPage);
            Header("Location: $loginPage");
        }
        $data['baseurl']=base_url();
        $data['title']="添加卖书信息-书易二手书";
        $bookid=$_GET['bookid'];
        $data['book']=$this->book_model->get_book_detail($bookid);
        $data['userinfo']=$this->user_model->get_userinfo($this->session->userdata('username'));
        $data['activetab']="none";
        $data['user_status']=$this->user_model->get_user_status(); //获取用户登录状态
        $this->load->view('addsalepost_view',$data);		//载入注册页面视图
    }

    /**
     * 执行添加书籍出售广告的控制器
     */
    function do_addsalepost(){
        $postsdata['user_id']=$this->session->userdata('username');
        $postsdata['book_id']=$_POST['bookid'];
        $postsdata['book_title']=$_POST['book_title'];
        $postsdata['price']=$_POST['price'];
        $postsdata['quality']=$_POST['quality'];
        $postsdata['other']=$_POST['other'];
        $postsdata['phone']=$_POST['phone'];
        $postsdata['email']=$_POST['email'];
        $postsdata['qq']=$_POST['qq'];
        $school=$_POST['school_initials'];
        if($postsdata['book_title']!=null&&$postsdata['book_id']!=null&&is_numeric($postsdata['price'])){
              $status=$this->post_model->insert_salepost($postsdata,$school);
              if($status==1){
                  $home=base_url();
                  Header("Location: $home");
              }
              else{
                  $data['baseurl']=base_url();
                  $data['title']="卖旧书-书易";
                  $bookid=$_POST['bookid'];
                  $data['book']=$this->book_model->get_book_detail($bookid);
                  $data['userinfo']=$this->user_model->get_userinfo($this->session->userdata('username'));
                  $data['activetab']="none";
                  $data['user_status']=$this->user_model->get_user_status(); //获取用户登录状态
                  $data['error']="添加卖书信息失败，请检查输入或者向管理员报告错误。";
                  $this->load->view('addsalepost_view',$data);
              }
         }
        else{
             $data['error']="错误：";
              if(!is_numeric($postsdata['price']))
                 $data['error'].="卖价必填并且只能是数字";
             $data['baseurl']=base_url();
             $data['title']="卖出旧书-书易";
             $bookid=$_POST['bookid'];
             $data['book']=$this->book_model->get_book_detail($bookid);
             $data['userinfo']=$this->user_model->get_userinfo($this->session->userdata('username'));
             $data['activetab']="none";
             $data['user_status']=$this->user_model->get_user_status(); //获取用户登录状态
             $this->load->view('addsalepost_view',$data);
          }
    }

     /**
     * 调用添加书籍求购广告的控制器
     */
    function wantedpost(){
        $this->load->library('session');
        $siteurl=site_url();
        $currentPage=$_SERVER['SERVER_NAME'].$_SERVER['PHP_SELF'].'?'.$_SERVER['QUERY_STRING'];
        if(($this->session->userdata('username'))==""){
            $loginPage=$siteurl.'/user/login?continue=yes&currentpage='.urlencode($currentPage);
            Header("Location: $loginPage");
        }
        $data['baseurl']=base_url();
        $data['title']="求购旧书-书易";
        $bookid=$_GET['bookid'];
        $data['book']=$this->book_model->get_book_detail($bookid);
        $data['userinfo']=$this->user_model->get_userinfo($this->session->userdata('username'));
        $data['activetab']="none";
        $data['user_status']=$this->user_model->get_user_status(); //获取用户登录状态
        $this->load->view('addwantedpost_view',$data);		//载入注册页面视图
    }

    /**
     * 执行添加书籍求购广告的控制器
     */
    function do_addwantedpost(){
        $postsdata['user_id']=$this->session->userdata('username');
        $postsdata['book_id']=$_POST['bookid'];
        $postsdata['book_title']=$_POST['book_title'];
        $postsdata['start_price']=$_POST['start_price'];
        $postsdata['end_price']=$_POST['end_price'];
        $postsdata['other']=$_POST['other'];
        $postsdata['phone']=$_POST['phone'];
        $postsdata['email']=$_POST['email'];
        $postsdata['qq']=$_POST['qq'];
        $school=$_POST['school_initials'];
        if($postsdata['book_title']!=null&&$postsdata['book_id']!=null&&is_numeric($postsdata['start_price'])
          &&is_numeric($postsdata['end_price'])&&$postsdata['start_price']<=$postsdata['end_price']){
              $status=$this->post_model->insert_wantedpost($postsdata,$school);
              if($status==1){
                  $home=base_url();
                  Header("Location: $home");
              }
              else{
                  $data['baseurl']=base_url();
                  $data['title']="求购旧书-书易";
                  $bookid=$_POST['bookid'];
                  $data['book']=$this->book_model->get_book_detail($bookid);
                  $data['userinfo']=$this->user_model->get_userinfo($this->session->userdata('username'));
                  $data['activetab']="none";
                  $data['user_status']=$this->user_model->get_user_status(); //获取用户登录状态
                  $data['error']="添加求购信息失败，请检查输入或者向管理员报告错误。";
                  $this->load->view('addwantedpost_view',$data);		
              }
         }
        else{
             $data['error']="错误 ";
              if(!is_numeric($postsdata['end_price'])||!is_numeric($postsdata['start_price']))
                 $data['error'].="价格范围必填并且只能是数字";
             if($postsdata['start_price']>$postsdata['end_price'])
                $data['error'].="最低价不能高于最高价";
             $data['baseurl']=base_url();
             $data['title']="求购旧书-书易";
             $bookid=$_POST['bookid'];
             $data['book']=$this->book_model->get_book_detail($bookid);
             $data['userinfo']=$this->user_model->get_userinfo($this->session->userdata('username'));
             $data['activetab']="none";
             $data['user_status']=$this->user_model->get_user_status(); //获取用户登录状态
             $this->load->view('addwantedpost_view',$data);
          }
    }

    /**
     * 在用户中心中显示用户自己销售出和求购到的书籍
     */
    function bookdealed(){
       if($this->session->userdata('username')==""){
               $siteurl=site_url();
               $currentPage=$_SERVER['SERVER_NAME'].$_SERVER['PHP_SELF'].'?'.$_SERVER['QUERY_STRING'];
               $loginPage=$siteurl.'/user/login?continue=yes&currentpage='.urlencode($currentPage);
               header("Location: $loginPage");
        }
        $type=$_GET['type'];
        $userid=$_GET['userid'];
        $bookid=$_GET['bookid'];
        $school=$this->user_model->get_school($userid);
        $this->post_model->bookdealed($type,$userid,$bookid,$school);
        
        //配置分页函数
        if(isset($_GET['curpage']))
           $offset=$_GET['curpage'];
        else
           $offset=0;
         if(!isset($_GET['totalrows'])){
           if($type=="sale")
             $result= $this->post_model->get_mysalepost(1,$userid,$school,0,$offset,8);  //total=1,dealed=1,perpage=8
           else if($type=="wanted")
             $result= $this->post_model->get_mywantedpost(1,$userid,$school,0,$offset,8);  //total=1,dealed=1,perpage=8
           $config['total_rows'] =$result[0]->number;
        }
        else{
           $config['total_rows'] =$_GET['totalrows'];
        }
        $config['page_query_string'] = TRUE;
        if($type=="sale")
            $link="user/my_salepost?go=yes&totalrows={$config['total_rows']}";
        else if($type=="wanted")
            $link="user/my_wantedpost?go=yes&totalrows={$config['total_rows']}";
        $config['base_url'] = site_url($link);
        $config['full_tag_open'] = '<p>';
        $config['full_tag_close'] = '</p>';
        $config['per_page']=8;
        $this->mypagination->initialize($config);
        $data['activetab']="myshop";
        $data['user_status']=$this->user_model->get_user_status(); //获取用户登录状态
        if($type=="sale"){
           $data['title']="我出售的书籍";
           $data['posts']=$this->post_model->get_mysalepost(0,$userid,$school,0,$offset,8);
           $data['activenav']="saleposts";
           $this->load->view('user/my_salepost_view',$data);
        }
        else if($type=="wanted"){
           $data['title']="我求购的书籍";
           $data['posts']=$this->post_model->get_mywantedpost(0,$userid,$school,0,$offset,8);
           $data['activenav']="wantedposts";
           $this->load->view('user/my_wantedpost_view',$data);
        }
    }

}
?>